<!--资产更新-->
<template>
  <div style="margin-top: 60px;margin-left:330px;width: 300px;height: 500px;border: 0px solid red;" >
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
      <el-form-item label="资产编号" prop="assetNo">
        <el-input v-model="ruleForm.assetNo"></el-input>
      </el-form-item>
      <el-form-item label="资产型号" prop="assetTypeNo">
        <el-input v-model="ruleForm.assetTypeNo"></el-input>
      </el-form-item>
      <el-form-item label="资产标签" prop="assetLabel">
        <el-input v-model="ruleForm.assetLabel"></el-input>
      </el-form-item>
      <el-form-item label="资产名" prop="assetName">
        <el-select v-model="ruleForm.assetName" placeholder="请选择资产名">
          <el-option label="类别1" value="category1"></el-option>
          <el-option label="类别2" value="category2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="资产类别" prop="assetCategory">
        <el-select v-model="ruleForm.assetCategory" placeholder="请选择资产类别">
          <el-option label="类别1" value="category1"></el-option>
          <el-option label="类别2" value="category2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="资产所属组织" prop="belongOrg">
        <el-select v-model="ruleForm.belongOrg" placeholder="请选择资产所属组织">
          <el-option v-for="item in OrgList" :label="item.orgName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资产负责人" prop="assetUser">
        <el-select v-model="ruleForm.assetUser" placeholder="请选择资产负责人">
          <el-option v-for="item in tbAssetRoleList" :label="item.username" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>
<script>
export default {
  data() {
    return {
      category:null,
      ruleForm: {
        assetNo: '',
        assetTypeNo: '',
        assetLabel: '',
        assetName:'',
        assetCategory: '',
        belongOrg: '',
        assetUser: '',
      },
      tbAssetRoleList: '',
      OrgList: '',
      rules: {
        assetNo: [
          { required: true, message: '请输入资产编号', trigger: 'blur' }
        ],
        assetTypeNo: [
          { required: true, message: '请输入资产型号', trigger: 'blur' }
        ],
        assetLabel: [
          { required: true, message: '请输入标签', trigger: 'blur' }
        ],
        assetName: [
          { required: true, message: '请选择资产名', trigger: 'change' }
        ],
        assetCategory: [
          { required: true, message: '请选择资产类别', trigger: 'change' }
        ],
        assetUser: [
          { required: true, message: '请选择资产负责人', trigger: 'change' }
        ],
        belongOrg: [
          { required: true, message: '请选择资产所属组织', trigger: 'change' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.put('http://localhost:8181/tbAsset/update',_this.ruleForm).then(function (resp) {
            if(resp.data.code == 0){
              _this.$alert(_this.ruleForm.assetName+'修改成功', '', {
                confirmButtonText: '确定',
                callback: action => {
                  _this.$router.push('/selectAsset3')
                }
              });
            }
          })
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    const _this = this
    axios.get('http://localhost:8181/tbAsset/findById/'+_this.$route.query.id).then(function (resp) {
      _this.ruleForm = resp.data.data
    });
    // 获取所属组织数据
    axios.get('http://localhost:8181/tbAssertOrg/list').then(function (resp) {
      console.log(resp.data);  // 确保控制台中打印了正确的数据
      _this.OrgList = resp.data.data;
    });

    // 获取资产负责人数据
    axios.get('http://localhost:8181/tbAssetRole/list').then(function (resp) {
      _this.tbAssetRoleList = resp.data.data;
    });
  }
}
</script>